namespace DotNetNuke.Services.Log.SiteLog
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class PurgeSiteLog : SchedulerClient
    {
        public PurgeSiteLog(ScheduleHistoryItem objScheduleHistoryItem)
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        public override void DoWork()
        {
            try
            {
                this.Progressing();
                this.PurgeSiteLogPlus();
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Site Log purged.");
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Site Log purge failed. " + exc.ToString());
                this.Errored(ref exc);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
        }

        private void PurgeSiteLogPlus()
        {
            SiteLogController objSiteLog = new SiteLogController();
            ArrayList arrPortals = new PortalController().GetPortals();
            int refIntHelperL0 = arrPortals.Count - 1;
            for (int intIndex = 0; intIndex <= refIntHelperL0; intIndex++)
            {
                PortalInfo objPortal = (PortalInfo) arrPortals[intIndex];
                if (objPortal.SiteLogHistory > 0)
                {
                    DateTime PurgeDate = DateAndTime.DateAdd(DateInterval.Day, (double) (0 - objPortal.SiteLogHistory), DateAndTime.Now);
                    objSiteLog.DeleteSiteLog(PurgeDate, objPortal.PortalID);
                }
            }
        }
    }
}

